package com.gzyj.jtt808.v2019.protocol.yuebiao;


import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_AddtionalInfo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lhj
 */
@Data
@ApiModel("驾驶员状态监测系统")
public class JTT808_0x0200_0x65 extends JTT808_AddtionalInfo implements Serializable {

    @ApiModelProperty("报警ID")
    public Long AlarmId;

    @ApiModelProperty("标志状态")
    public Byte FlagState;

    @ApiModelProperty("报警/事件类型")
    public Byte AlarmOrEventType;
    /// <summary>
    ///  解析后
    /// </summary>
    @ApiModelProperty("报警/事件类型")
    public String AlarmOrEventTypeString;
    @ApiModelProperty("报警级别")
    public Byte AlarmLevel;
    @ApiModelProperty("疲劳程度")
    public Byte Fatigue;

    @ApiModelProperty("预留")
    public byte[] Retain = new byte[4];

    @ApiModelProperty("车速")
    public Byte Speed;

    @ApiModelProperty("高程")
    public Integer Altitude;

    @ApiModelProperty("纬度")
    public Long Latitude;

    @ApiModelProperty("经度")
    public Long Longitude;
    /// <summary>
    ///
    /// YYMMDDhhmmss
    /// BCD[6]
    /// </summary>
    @ApiModelProperty("日期时间")
    public Date AlarmTime;

    @ApiModelProperty("车辆状态")
    public Integer VehicleState;

    @ApiModelProperty("报警标识号")
    public AlarmIdentificationProperty AlarmIdentification;

    public String AlarmIdentificationStr;

    public JTT808_0x0200_0x65() {
        super(0x65);
        this.setLength(71);
    }

    @Override
    public void encode(ByteBuf buf) {
        buf.writeByte(this.getID());
        buf.writeByte(this.getLength());

        buf.writeInt(this.AlarmId.intValue());
        buf.writeByte(this.FlagState);
        buf.writeByte(this.AlarmOrEventType);
        buf.writeByte(this.AlarmLevel);
        buf.writeByte(this.Fatigue);
        buf.writeBytes(this.Retain);
        buf.writeByte(this.Speed);
        buf.writeShort(this.Altitude);
        buf.writeInt(this.Latitude.intValue());
        buf.writeInt(this.Longitude.intValue());
        buf.writeBytes(HexBytesUtil.WriteDateTime_yyMMddHHmmss(this.AlarmTime));
        buf.writeShort(this.VehicleState);
        this.AlarmIdentification.encode(buf);
    }

    @Override
    public JTT808_0x0200_0x65 decode(ByteBuf buf) {
        this.setLength(buf.readByte());

        this.AlarmId = buf.readUnsignedInt();
        this.FlagState = buf.readByte();
        this.AlarmOrEventType = buf.readByte();
        switch (this.AlarmOrEventType) {
            case 0x01:
                this.AlarmOrEventTypeString = "疲劳驾驶报警";
                break;
            case 0x02:
                this.AlarmOrEventTypeString = "接打手持电话报警";
                break;
            case 0x03:
                this.AlarmOrEventTypeString = "抽烟报警";
                break;
            case 0x04:
                this.AlarmOrEventTypeString = "不目视前方报警";
                break;
            case 0x05:
                this.AlarmOrEventTypeString = "驾驶员异常报警";
                break;
            case 0x06:
                this.AlarmOrEventTypeString = "探头遮挡报警";
                break;
            case 0x07:
                this.AlarmOrEventTypeString = "用户自定义";
                break;
            case 0x08:
                this.AlarmOrEventTypeString = "超时驾驶报警";
                break;
            case 0x09:
                this.AlarmOrEventTypeString = "用户自定义";
                break;
            case 0x0A:
                this.AlarmOrEventTypeString = "未系安全带报警";
                break;
            case 0x0B:
                this.AlarmOrEventTypeString = "红外阻断墨镜失效报警";
                break;
            case 0x0C:
                this.AlarmOrEventTypeString = "双脱把报警";
                break;
            case 0x0D:
                this.AlarmOrEventTypeString = "玩手机报警";
                break;
            case 0x0E:
            case 0x0F:
                this.AlarmOrEventTypeString = "用户自定义";
                break;
            case 0x10:
                this.AlarmOrEventTypeString = "自动抓拍事件";
                break;
            case 0x11:
                this.AlarmOrEventTypeString = "驾驶员变更事件";
                break;
            case 0x12:
            case 0x13:
            case 0x14:
                this.AlarmOrEventTypeString = "自动刹车";
                break;
            case 0x15:
                this.AlarmOrEventTypeString = "手动刹车";
                break;
            case 0x16:
                this.AlarmOrEventTypeString = "注意前方";
                break;
            case 0x17:
                this.AlarmOrEventTypeString = "注意车距";
                break;
            case 0x18:
            case 0x19:
            case 0x1A:
            case 0x1B:
            case 0x1C:
            case 0x1D:
            case 0x1E:
            case 0x1F:
                this.AlarmOrEventTypeString = "用户自定义";
                break;
        }
        this.AlarmLevel = buf.readByte();
        this.Fatigue = buf.readByte();
        this.Retain = HexBytesUtil.readByOffset(buf, 4);
        this.Speed = buf.readByte();
        this.Altitude = buf.readUnsignedShort();
        this.Latitude = buf.readUnsignedInt();
        this.Longitude = buf.readUnsignedInt();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            this.AlarmTime = sdf.parse(HexBytesUtil.ReadDateTime_yyMMddHHmmss(HexBytesUtil.readByOffset(buf, 6)));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.VehicleState = buf.readUnsignedShort();

        this.AlarmIdentification = new AlarmIdentificationProperty();
        this.AlarmIdentification.decode(buf);

        ByteBuf tmp= Unpooled.buffer(40);
        this.AlarmIdentification.encode(tmp);
        this.AlarmIdentificationStr= ByteBufUtil.hexDump(tmp);

        return this;
    }


}
